import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/parts/api/v1';

const getInitDesc = formatMessage({
    id: 'api.getInit',
    defaultMessage: '初始化'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询备件可提报资源单'
});

const getBatchDelete = formatMessage({
    id: 'api.getBatchDelete',
    defaultMessage: '批量删除'
});

const getImportData = formatMessage({
    id: 'api.getImportData',
    defaultMessage: '导入提交'
});

const getParts = formatMessage({
    id: 'api.getPartData',
    defaultMessage: '查询主机厂备件'
});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出'
});

// 初始化请求
export const getInitData = () =>
    // eslint-disable-next-line no-irregular-whitespace
    request(`${baseUrl}/ui/dealerPartResource​/init`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitDesc));

// 查询
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/dealerPartResources/forCurrentUser?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 批量删除
export const batchDelete = data => request(`${baseUrl}/dealerPartResources/batchDelete`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getBatchDelete));

//导入模板
export const importTemp = () => request(`${baseUrl}/dealerPartResources/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);

//导入提交
export const importData = data => request(`${baseUrl}/dealerPartResources/importSubmit`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getImportData));

// 主机厂备件查询
export const partResources = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forDealerPartResources?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getParts));
};

// 导出
export const exports = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealerPartResources/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};
